package rad.framework.mda.properties.views;

import org.eclipse.gmf.runtime.common.ui.services.properties.PropertiesService;
import org.eclipse.jface.util.Assert;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.ui.views.properties.tabbed.AbstractPropertySection;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.MultiplicityElement;
import org.eclipse.uml2.uml.UMLPackage;

/**
 * @author boudyacho
 *
 */
public class MultiplicityElementSection extends AbstractPropertySection {

	private Button upperBound;

	private MultiplicityElement umlElement;

	private CLabel upperLabel;
	
	/* (non-Javadoc)
	 * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#setInput(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
	 */
	public void setInput(IWorkbenchPart part, ISelection selection) {
		super.setInput(part, selection);
		Assert.isTrue(selection instanceof IStructuredSelection);
		Object input = ((IStructuredSelection) selection).getFirstElement();
		Assert.isTrue(input instanceof MultiplicityElement);
		this.umlElement = (MultiplicityElement) input;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#createControls(org.eclipse.swt.widgets.Composite, org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage)
	 */
	public void createControls(Composite parent,TabbedPropertySheetPage aTabbedPropertySheetPage) {
		
		super.createControls(parent, aTabbedPropertySheetPage);

		Composite composite = getWidgetFactory().createFlatFormComposite(parent);
		FormData data;

		Shell shell = new Shell();
		GC gc = new GC(shell);
		gc.setFont(shell.getFont());
		Point point = gc.textExtent("");
		int buttonHeight = point.y + 5;
		gc.dispose();
		shell.dispose();

		upperLabel = getWidgetFactory().createCLabel(composite, "Unlimited:"); 
		upperBound = getWidgetFactory().createButton(composite,"", SWT.CHECK); 
		upperBound.addSelectionListener(new SelectionAdapter() {

			public void widgetSelected(SelectionEvent event) {
				IPropertySource properties = (IPropertySource) getPropertySource(umlElement);
				if (upperBound.getSelection()) {
					properties.setPropertyValue(
							UMLPackage.Literals.MULTIPLICITY_ELEMENT__UPPER, new Integer(-1));
				} else {
					properties.setPropertyValue(
							UMLPackage.Literals.MULTIPLICITY_ELEMENT__UPPER, new Integer(1));
				}
			}
		});

		data = new FormData();
		data.left = new FormAttachment(0, 0);
		data.top = new FormAttachment(0, 0);
		upperLabel.setLayoutData(data);

		data = new FormData();
		data.left = new FormAttachment(0, STANDARD_LABEL_WIDTH);
		data.top = new FormAttachment(0, 0);
		upperBound.setLayoutData(data);

	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#refresh()
	 */
	public void refresh() {
		IPropertySource properties = getPropertySource(umlElement);
		Object value = properties
		.getPropertyValue(UMLPackage.Literals.MULTIPLICITY_ELEMENT__UPPER);
		if (value != null && "-1".equals(value.toString())) {
			upperBound.setSelection(true);
		} else {
			upperBound.setSelection(false);
		}
	}

	/**
	 * @param o
	 * @return
	 */
	protected IPropertySource getPropertySource(Object o) {
		return PropertiesService.getInstance().getPropertySource(o);
	}
}
